<template>
  <ul theme="dark" width="100%" class="nav_user">
    <li class="alert menu-item ant-menu-item" v-show="unread > 0" @click="showDrawer">
      <a-icon type="alert" class="icon" />
      <span class="text">{{unread}}</span>
    </li>
    <li class="avatar menu-item ant-menu-item">
      <a-avatar icon="user" class="ava" size="small" />
      <span @click="logout" class="text">注销</span>
    </li>
    <a-drawer
      title="通知中心"
      placement="right"
      :closable="true"
      :width="500"
      @close="onClose"
      :visible="visible"
      :afterVisibleChange="afterVisibleChange"
    >
      <div class="notice">
        <div class="head">
          <a @click="handleRead">清除全部</a>
        </div>
        <ul>
          <li v-for="(v,k) in notice" :key="k" :class="v.T==1?'red':''">
            <p>{{v.M}}</p>
            <span>{{v.D}}</span>
          </li>
        </ul>
      </div>
    </a-drawer>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      notice: [],
      unread: 0
    };
  },
  props: {
    isCollapsed: Boolean
  },
  watch: {
    $route() {
      this.loadNotice();
    }
  },
  created() {
    this.loadNotice();
  },
  methods: {
    logout() {
      this.$api.auth.logout();
    },
    loadNotice() {
      this.$api.logs.getNoticeList().then(res => {
        this.notice = res.list;
        this.unread = res.unread;
      });
    },
    afterVisibleChange(val) {
      console.log("visible", val);
    },
    showDrawer() {
      this.visible = true;
    },
    onClose() {
      this.visible = false;
    },
    handleRead() {
      this.$api.logs.getNoticeList({ ac: "read" }).then(res => {
        this.notice = res.list;
        this.unread = res.unread;
      });
    }
  }
};
</script>

<style lang="less" scoped>
.nav_user {
  color: #fff;
  position: absolute;
  right: 20px;
  top: 0;
  li.alert {
    display: inline-block;    
    color: rgb(0, 255, 20);
    padding-left: 10px;
    /deep/ .anticon {
      vertical-align: -5px;
      font-size: 20px;
    }
    /deep/ .text {
      font-size: 20px;
    }
  }
  li.avatar {
    display: inline-block;
    padding-left: 16px;
    .ava {
      margin-right: 10px;
    }
    .ava /deep/ .anticon {
      font-size: 18px;
      margin: 0;
    }
  }
  span.text {
    display: inline-block;
    overflow: hidden;
    //max-width: 70px;
    //text-overflow: ellipsis;//...
    white-space: nowrap;
    vertical-align: middle;
    transition: width 0.2s ease;
    transition: opacity 0.2s ease;
    font-size: 16px;
    opacity: 1;
  }
}
.notice {
  .head {
    margin-bottom: 10px;
  }
  li {
    &.red p {
      color: #ff0000;
      margin-bottom: 5px;
    }
    padding-bottom: 20px;
  }
}
</style>